package com.obas.business.taskexecution.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ease.platform.authorization.service.impl.AbstractAuthorizationServiceImpl;
import com.obas.access.business.service.SystemFunctionAccessService;
import com.obas.access.business.service.TestPlanAccessService;
import com.obas.access.business.service.TestPlanItemAccessService;
import com.obas.access.business.service.TestTaskAccessService;
import com.obas.access.business.service.TestTaskItemAccessService;
import com.obas.access.business.service.TestcaseScriptAccessService;
import com.obas.business.common.vo.TestContext;
import com.obas.business.parameterpool.service.ParameterPoolManagementService;
import com.obas.business.parameterpool.vo.ParameterPoolGrid;
import com.obas.business.taskexecution.service.TaskExecutionManagementService;
import com.obas.business.taskexecution.vo.SystemFunctionObject;
import com.obas.business.taskexecution.vo.TestPlanObject;
import com.obas.model.business.SystemFunction;
import com.obas.model.business.TestPlan;
import com.obas.model.business.TestPlanItem;
import com.obas.model.business.TestTask;
import com.obas.model.business.TestcaseScript;
import com.obas.monitor.TestContextManager;
import com.obas.monitor.TestPlanPlayer;

public class TaskExecutionManagementServiceImpl extends AbstractAuthorizationServiceImpl implements TaskExecutionManagementService {

    private SystemFunctionAccessService systemFunctionAccessService;

    private ParameterPoolManagementService parameterPoolManagementService;

    private TestPlanAccessService testPlanAccessService;

    private TestPlanItemAccessService testPlanItemAccessService;

    private TestTaskAccessService testTaskAccessService;

    private TestcaseScriptAccessService testcaseScriptAccessService;

    private TestTaskItemAccessService testTaskItemAccessService;

    public TestPlan createTestPlan(TestPlanObject testPlanObject) {
        TestPlan testPlan = testPlanObject.getTestPlan();

        if (null != testPlan) {
            testPlan = testPlanAccessService.create(testPlan);
            List<TestPlanItem> testPlanItems =  testPlanObject.getTestPlanItems();            
//            long[] parameterIds = testPlanObject.getParameterIds();
            if (notEmpty(testPlanItems)) {
                for (TestPlanItem testPlanItem  : testPlanItems) {
                    testPlanItem.setTestPlanId(testPlan.getId());
                    long parameterId = testPlanItem.getParameterId();                   
                    TestcaseScript testcaseScript = parameterPoolManagementService.findTestcaseScriptByParameterId(parameterId);
                    if (null != testcaseScript) {
                        testPlanItem.setTestcaseScriptId(testcaseScript.getId());
                    }
                    testPlanItemAccessService.create(testPlanItem);
                }
            }
        }
        return testPlan;
    }

    public List<SystemFunctionObject> listSystemFunctionObject() {
        List<SystemFunction> systemFunctions = systemFunctionAccessService.findAll();
        if (null != systemFunctions && notEmpty(systemFunctions)) {
            List<SystemFunctionObject> systemFunctionObjects = new ArrayList<SystemFunctionObject>();

            for (SystemFunction systemFunction : systemFunctions) {
                SystemFunctionObject systemFunctionObject = new SystemFunctionObject();
                systemFunctionObject.setSystemFunction(systemFunction);

                ParameterPoolGrid parameterPoolGrid = parameterPoolManagementService.findByTestcaseScriptId(systemFunction.getTestcaseScriptId());
                systemFunctionObject.setParameterPoolGrid(parameterPoolGrid);

                TestcaseScript testcaseScript = testcaseScriptAccessService.findById(systemFunction.getTestcaseScriptId());
                systemFunctionObject.setTestcaseScript(testcaseScript);
                systemFunctionObjects.add(systemFunctionObject);
            }
            return systemFunctionObjects;
        }
        return null;

    }

    @Override
    public void runTestPlan(String pageId, long id) {
       List<TestPlanItem> items = testPlanItemAccessService.findByTestPlanId(id);
       for (TestPlanItem testPlanItem : items) {
          TestcaseScript script = testcaseScriptAccessService.findById(testPlanItem.getTestcaseScriptId());
          testPlanItem.setTestcaseScript(script);
       }
       TestContext context = TestContextManager.getTestContext(pageId);
       //create TestTask
       TestTask task = testTaskAccessService.createTaskByTestPlanId(id);
       context.testTaskId = task.getId();
       context.playType = TestContext.RUN;
       TestPlanPlayer tp = new TestPlanPlayer(context, items, testTaskAccessService, testTaskItemAccessService);
       Thread thread = new Thread(tp);
       thread.start();

    }

    /**
     * @return the systemFunctionAccessService
     */
    public SystemFunctionAccessService getSystemFunctionAccessService() {
        return systemFunctionAccessService;
    }

    /**
     * @param systemFunctionAccessService
     *            the systemFunctionAccessService to set
     */
    public void setSystemFunctionAccessService(SystemFunctionAccessService systemFunctionAccessService) {
        this.systemFunctionAccessService = systemFunctionAccessService;
    }

    /**
     * @return the parameterPoolManagementService
     */
    public ParameterPoolManagementService getParameterPoolManagementService() {
        return parameterPoolManagementService;
    }

    /**
     * @param parameterPoolManagementService
     *            the parameterPoolManagementService to set
     */
    public void setParameterPoolManagementService(ParameterPoolManagementService parameterPoolManagementService) {
        this.parameterPoolManagementService = parameterPoolManagementService;
    }

    /**
     * @return the testPlanAccessService
     */
    public TestPlanAccessService getTestPlanAccessService() {
        return testPlanAccessService;
    }

    /**
     * @param testPlanAccessService the testPlanAccessService to set
     */
    public void setTestPlanAccessService(TestPlanAccessService testPlanAccessService) {
        this.testPlanAccessService = testPlanAccessService;
    }

    /**
     * @return the testPlanItemAccessService
     */
    public TestPlanItemAccessService getTestPlanItemAccessService() {
        return testPlanItemAccessService;
    }

    /**
     * @param testPlanItemAccessService the testPlanItemAccessService to set
     */
    public void setTestPlanItemAccessService(TestPlanItemAccessService testPlanItemAccessService) {
        this.testPlanItemAccessService = testPlanItemAccessService;
    }

    public TestTaskAccessService getTestTaskAccessService() {
        return testTaskAccessService;
    }

    public void setTestTaskAccessService(TestTaskAccessService testTaskAccessService) {
        this.testTaskAccessService = testTaskAccessService;
    }

    public TestcaseScriptAccessService getTestcaseScriptAccessService() {
        return testcaseScriptAccessService;
    }

    public void setTestcaseScriptAccessService(
            TestcaseScriptAccessService testcaseScriptAccessService) {
        this.testcaseScriptAccessService = testcaseScriptAccessService;
    }

    public TestTaskItemAccessService getTestTaskItemAccessService() {
        return testTaskItemAccessService;
    }

    public void setTestTaskItemAccessService(
            TestTaskItemAccessService testTaskItemAccessService) {
        this.testTaskItemAccessService = testTaskItemAccessService;
    }

}
